package com.draby.starfantasymaze.activities.launch;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Window;

import com.draby.starfantasymaze.R;
import com.draby.starfantasymaze.activities.main.MainActivity;
import com.draby.starfantasymaze.dto.Achievement;
import com.draby.starfantasymaze.utils.FileUtil;
import com.draby.starfantasymaze.utils.SystemUiUtil;

import java.util.Timer;
import java.util.TimerTask;

public class LaunchActivity extends AppCompatActivity {

    // 文件存放父目录与文件名
    private String parent;
    private String data_fileName;
    private String achievement_fileName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 隐藏 状态栏 与 导航栏
        SystemUiUtil.hideSystemBar(getWindow());
        setContentView(R.layout.activity_launch);

        // 保存当前上下文
        if (!FileUtil.saveContext(getApplicationContext())) {
            // 初始化失败，弹出提示信息
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提示").setMessage("出现了未知错误，请联系制作者😊").setCancelable(false)
                    .setPositiveButton("确定", (dialog, which) -> {
                        // 退出程序
                        finish();
                    }).show();
        }
        // 判断是否为第一次启动 app
        parent = getString(R.string.file_directory);
        data_fileName = getString(R.string.fileName_data);
        achievement_fileName = getString(R.string.fileName_achievement);
        String firstLand = FileUtil.getValue(parent, data_fileName, "firstLand");
        if (firstLand == null){
            // 文件不存在，即第一次启动，初始化默认数据文件
            boolean success = FileUtil.initData(parent, data_fileName) && FileUtil.initAchievements(parent, achievement_fileName);
            if (!success) {
                // 数据初始化失败，弹出提示信息
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("提示").setMessage("数据初始化失败，请联系制作者😊").setCancelable(false)
                        .setPositiveButton("确定", (dialog, which) -> {
                            // 退出程序
                            finish();
                        }).show();
            }
            // 初始化成功后执行
            FileUtil.setValue(parent, data_fileName, "firstLand", String.valueOf(false));
            // 首次启动成就完成
            FileUtil.finishAchievementByName(parent, achievement_fileName, "BabyDragon");
        }
        // 延迟时间后跳转到主界面
        startMainActivity();
    }

    private void startMainActivity() {
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                Intent mainIntent = new Intent(LaunchActivity.this, MainActivity.class);
                startActivity(mainIntent);
                finish();
            }
        };
        Timer timer = new Timer();
        timer.schedule(task, 3000);
    }
}